package org.argeproje.resim.proc.filter;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;

import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ImageDA;



public class EdgePR extends GradientMagnitudePR {
	public final static int MASK_SOBEL = 0;
	public final static int MASK_ROBERTS = 1;
	public final static int MASK_PREWITT = 2;
	public final static int MASK_FREI_CHEN = 3;
	private final float[] H_KERNEL_SOBEL = 
		new float[] {1.0F, 0.0F, -1.0F,
		          2.0F, 0.0F, -2.0F,
		          1.0F, 0.0F, -1.0F
		};
	private final float[] V_KERNEL_SOBEL = 
		new float[] {-1.0F, -2.0F, -1.0F,
		          0.0F, 0.0F, 0.0F,
		          1.0F, 2.0F, 1.0F
		};
	private final float[] H_KERNEL_ROBERT = 
		new float[] {0.0F, 0.0F, -1.0F,
		          0.0F, 1.0F, 0.0F,
		          0.0F, 0.0F, 0.0F
		};
	private final float[] V_KERNEL_ROBERT = 
		new float[] {-1.0F, 0.0F, 0.0F,
		          0.0F, 1.0F, 0.0F,
		          0.0F, 0.0F, 0.0F
		};
	private final float[] H_KERNEL_PREWITT = 
		new float[] {1.0F, 0.0F, -1.0F,
		          1.0F, 0.0F, -1.0F,
		          1.0F, 0.0F, -1.0F
		};
	private final float[] V_KERNEL_PREWITT = 
		new float[] {-1.0F, -1.0F, -1.0F,
		          0.0F, 0.0F, 0.0F,
		          1.0F, 1.0F, 1.0F
		};
	private final float[] H_KERNEL_FREI_CHEN = 
		new float[] {1.0F, 0.0F, -1.0F,
		          1.414F, 0.0F, -1.414F,
		          1.0F, 0.0F, -1.0F
		};
	private final float[] V_KERNEL_FREI_CHEN = 
		new float[] {-1.0F, -1.414F, -1.0F,
		          0.0F, 0.0F, 0.0F,
		          1.0F, 1.414F, 1.0F
		};
	private int _mask = MASK_SOBEL;
	public void setParameters(int mask){
		setParamMask(mask);
	}
	public void setParamMask(int mask){
		_mask = mask;
	}
	public int getParamMask(){
		return _mask;
	}
	protected float[] getKernelH(){
		float[] kernel = H_KERNEL_SOBEL;
		switch (getParamMask()) {
		case MASK_ROBERTS:
			kernel = H_KERNEL_ROBERT;
			break;
		case MASK_PREWITT:
			kernel = H_KERNEL_PREWITT;
		case MASK_FREI_CHEN:
			kernel = H_KERNEL_FREI_CHEN;
		default:
			break;
		}
		return kernel;
	}
	protected float[] getKernelV(){
		float[] kernel = V_KERNEL_SOBEL;
		switch (getParamMask()) {
		case MASK_ROBERTS:
			kernel = V_KERNEL_ROBERT;
			break;
		case MASK_PREWITT:
			kernel = V_KERNEL_PREWITT;
		case MASK_FREI_CHEN:
			kernel = V_KERNEL_FREI_CHEN;
		default:
			break;
		}
		return kernel;
	}
	public Data process() {
		KernelJAI kernelJAIH = new KernelJAI(3, 3, getKernelH());
		KernelJAI kernelJAIV = new KernelJAI(3, 3, getKernelV());
		PlanarImage i = getInputAsPlanarImage();
		PlanarImage o = JAI.create("gradientmagnitude", i, kernelJAIH, kernelJAIV);
		ImageDA out = new ImageDA(o);
		setOutput((Data)out);	
		return getOutput();
	}
}
